package org.jeecg.modules.flower.service.impl;

import org.jeecg.modules.flower.entity.FlowerAfSalesMain;
import org.jeecg.modules.flower.entity.FlowerAfSalesDetal;
import org.jeecg.modules.flower.mapper.FlowerAfSalesDetalMapper;
import org.jeecg.modules.flower.mapper.FlowerAfSalesMainMapper;
import org.jeecg.modules.flower.service.IFlowerAfSalesMainService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 售后单主表
 * @Author: jeecg-boot
 * @Date:   2022-07-15
 * @Version: V1.0
 */
@Service
public class FlowerAfSalesMainServiceImpl extends ServiceImpl<FlowerAfSalesMainMapper, FlowerAfSalesMain> implements IFlowerAfSalesMainService {

	@Autowired
	private FlowerAfSalesMainMapper flowerAfSalesMainMapper;
	@Autowired
	private FlowerAfSalesDetalMapper flowerAfSalesDetalMapper;
	
	@Override
	@Transactional
	public void saveMain(FlowerAfSalesMain flowerAfSalesMain, List<FlowerAfSalesDetal> flowerAfSalesDetalList) {
		flowerAfSalesMainMapper.insert(flowerAfSalesMain);
		if(flowerAfSalesDetalList!=null && flowerAfSalesDetalList.size()>0) {
			for(FlowerAfSalesDetal entity:flowerAfSalesDetalList) {
				//外键设置
				entity.setMainid(flowerAfSalesMain.getId());
				flowerAfSalesDetalMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(FlowerAfSalesMain flowerAfSalesMain,List<FlowerAfSalesDetal> flowerAfSalesDetalList) {
		flowerAfSalesMainMapper.updateById(flowerAfSalesMain);
		
		//1.先删除子表数据
		flowerAfSalesDetalMapper.deleteByMainId(flowerAfSalesMain.getId());
		
		//2.子表数据重新插入
		if(flowerAfSalesDetalList!=null && flowerAfSalesDetalList.size()>0) {
			for(FlowerAfSalesDetal entity:flowerAfSalesDetalList) {
				//外键设置
				entity.setMainid(flowerAfSalesMain.getId());
				flowerAfSalesDetalMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		flowerAfSalesDetalMapper.deleteByMainId(id);
		flowerAfSalesMainMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			flowerAfSalesDetalMapper.deleteByMainId(id.toString());
			flowerAfSalesMainMapper.deleteById(id);
		}
	}
	
}
